package com.victor.controller.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.entity.Age;
import com.victor.entity.Banner;
import com.victor.entity.BannerToGrowplan;
import com.victor.entity.Picturebook;
import com.victor.service.BannerService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * Banner管理
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

	@Autowired
	private BannerService bannerService;

	// 获取banner图列表
	@RequestMapping("/getAllBanner")
	public Msg getAllBanner() {
		List<Banner> bannerlist = bannerService.getAllBanner();
		return Msg.success().add("bannerlist", bannerlist);
	}

	// 修改banner信息
	@RequestMapping("/updateBanner")
	public Msg updateBanner(Banner banner, MultipartFile banner_photopath_file, MultipartFile introduce_photopath_file,
			HttpServletRequest request) throws Exception {
		if (banner.getId() == 0) {
			return Msg.fail("参数错误");
		}
		if (banner_photopath_file != null) {
			String banner_photopath = FileUpload.uploadFile(banner_photopath_file, request, "photo");
			banner.setBanner_photopath(banner_photopath);
		}
		if (introduce_photopath_file != null) {
			String introduce_photopath = FileUpload.uploadFile(introduce_photopath_file, request, "photo");
			banner.setIntroduce_photopath(introduce_photopath);
		}
		boolean updResult = bannerService.updateBanner(banner);
		return Msg.judge(updResult);
	}

	// 获取banner详情
	@RequestMapping("/getBannerXqByid")
	public Msg getBannerXqByid(String bannerid) {
		Banner banner = bannerService.getBannerXqByid(bannerid);
		if (banner != null) {
			return Msg.success().add("banner", banner);
		}
		return Msg.fail();
	}

	// 获取年龄段列表
	@RequestMapping("/getAllAgeList")
	public Msg getAllAgeList() {

		List<Age> agelist = bannerService.getAllAgeList();
		if (agelist != null) {
			return Msg.success().add("agelist", agelist);
		}
		return Msg.fail("暂无年龄段列表");
	}

	// 用bannerid和ageid查询绘本列表和年龄段关系
	@RequestMapping("/getPicturebookByBannerAndAge")
	public Msg getPicturebookByBannerAndAge(String bannerid, String ageid) {
		// 创建一个返回map
		Map<String, Object> map = new HashMap<String, Object>();
		// 创建一个单月绘本list
		List<Picturebook> list = new ArrayList<>();
		// 拿到成长计划表
		List<BannerToGrowplan> bannerToGrowplans = bannerService.getPicturebookByBannerAndAge(bannerid, ageid);
		for (int i = 1; i < 13; i++) {
			for (BannerToGrowplan bannerToGrowplan : bannerToGrowplans) {
				if (String.valueOf(i).equals(bannerToGrowplan.getMonth())) {
					list.add(bannerToGrowplan.getPicturebook());
				}
			}
			map.put(String.valueOf(i), list);
			list = new ArrayList<>();
		}

		return Msg.success().add("picturebooks", map);
	}

	// 用bannerid和ageid查询绘本列表和年龄段关系
	@RequestMapping("/getPicturebookByBannerAndAge2")
	public Msg getPicturebookByBannerAndAge2(String bannerid, String ageid) {
		// 创建一个返回map
		Map<String, Object> map = new HashMap<String, Object>();
		// 创建一个单月绘本list
		List<BannerToGrowplan> list = new ArrayList<BannerToGrowplan>();
		// 拿到成长计划表
		List<BannerToGrowplan> bannerToGrowplans = bannerService.getPicturebookByBannerAndAge(bannerid, ageid);
		for (int i = 1; i < 13; i++) {
			for (BannerToGrowplan bannerToGrowplan : bannerToGrowplans) {
				if (String.valueOf(i).equals(bannerToGrowplan.getMonth())) {
					list.add(bannerToGrowplan);
				}
			}
			map.put(String.valueOf(i), list);
			list = new ArrayList<>();
		}

		return Msg.success().add("picturebooks", map);
	}

	// 添加绘本 传添加到那个年龄,添加的banner图,添加的月份,图书id
	@RequestMapping("/addBannerPicture")
	public Msg addBannerPicture(BannerToGrowplan bannertogrowplan) {
		// 通过四个参数去查询是否有数据
		BannerToGrowplan dataBannerToGrowplan = bannerService.getBannerToGrowplanByAll(bannertogrowplan);
		if (dataBannerToGrowplan != null) {
			return Msg.fail("已选过该绘本");
		}
		boolean insResult = bannerService.addBannerPicture(bannertogrowplan);
		if (insResult) {
			return Msg.success();
		}
		return Msg.fail("添加绘本失败");
	}

	// 批量删除 通过返回列表的pid进行删除 哪个年龄,添加的banner图,添加的月份,图书id
	@RequestMapping("/delBannerPicturebook")
	public Msg delBannerPicturebook(@RequestBody List<BannerToGrowplan> bannertogrowplans) {
		for (BannerToGrowplan bannertogrowplan : bannertogrowplans) {
			// 通过四个参数去查询是否有数据
			BannerToGrowplan dataBannerToGrowplan = bannerService.getBannerToGrowplanByAll(bannertogrowplan);
			if (dataBannerToGrowplan != null) {
				// 删除banner中绘本
				boolean delResult = bannerService.delBannerPicturebook(dataBannerToGrowplan.getId());
				if (delResult) {
					return Msg.success();
				}
				return Msg.fail();
			}
		}
		return Msg.fail("参数错误");
	}

	// 批量删除 通过id数组
	@RequestMapping("/delBannerPicturebookByids")
	public Msg delBannerPicturebookByids(int ids) {
		// 删除banner中绘本
		boolean delResult = bannerService.delBannerPicturebookByids(ids);
		if (delResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

}
